/*
 * @Author: szx
 * @Date: 2022-03-05 14:21:10
 * @LastEditTime: 2022-03-05 14:30:32
 * @Description:
 * @FilePath: \leetcode\100-199\106\106_2.js
 */
function TreeNode(val, left, right) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
}

/**
 * @param {number[]} inorder
 * @param {number[]} postorder
 * @return {TreeNode}
 */

var buildTree = function (inorder, postorder) {
    if (!postorder.length) return null;
    const val = postorder.pop();
    const root = new TreeNode(val);
    const inorderRoot = inorder.indexOf(val);
    root.left = buildTree(inorder.slice(0, inorderRoot + 1), postorder.slice(0, inorderRoot));
    root.right = buildTree(inorder.slice(inorderRoot + 1), postorder.slice(inorderRoot));
    return root;
};
